import cv2
import numpy as np
from matplotlib import pyplot as plt
import random,time

from PIL import ImageGrab
import pyautogui
import cv2
import numpy as np
import time
# from matplotlib import pyplot as plt

cs = cv2.imread('./images/cs.png', 0)#测试
zd = cv2.imread('./images/zd.jpg', 0)#组队
yqfy = cv2.imread('./images/yqfy.png', 0)#妖气封印
jr = cv2.imread('./images/jr.jpg', 0)#进入
zb = cv2.imread('./images/zb.jpg', 0)#准备
js = cv2.imread('./images/js.jpg', 0)#结束
sb = cv2.imread('./images/sb.jpg', 0)#失败
yh = cv2.imread('./images/yh.jpg', 0)#御魂
ts = cv2.imread('./images/ts.jpg', 0)#探索
bqds = cv2.imread('./images/bqds.jpg', 0)#八岐大蛇
zd1 = cv2.imread('./images/zd1.png', 0)#组队1
cjdw = cv2.imread('./images/cjdw.png', 0)#创建队伍
cj = cv2.imread('./images/cj.png', 0)#创建
kszd = cv2.imread('./images/kszd.png', 0)#开始战斗
qd = cv2.imread('./images/qd.png', 0)#确定
jjtp = cv2.imread('./images/jjtp.png', 0)#结界突破
jg = cv2.imread('./images/jg.png', 0)#jg
yq = cv2.imread('./images/yq.png', 0)#邀请
di2zhang = cv2.imread('./images/di2zhang.png', 0)#第二章

di3zhang = cv2.imread('./images/di3zhang.png', 0)#第二章
di4zhang = cv2.imread('./images/di4zhang.png', 0)#第二章
di5zhang = cv2.imread('./images/di5zhang.png', 0)#第二章

di20zhang = cv2.imread('./images/di20zhang.png', 0)#第二章
di21zhang = cv2.imread('./images/di21zhang.png', 0)#第二章
di22zhang = cv2.imread('./images/di22zhang.png', 0)#第二章
di23zhang = cv2.imread('./images/di23zhang.png', 0)#第二章

ts1 = cv2.imread('./images/ts1.png', 0)#探索
daotubiao = cv2.imread('./images/daotubiao.png', 0)#怪物刀图标
daotubiao1 = cv2.imread('./images/daotubiao1.png', 0)#怪物BOOS刀图标
zhanliping = cv2.imread('./images/zhanliping.png', 0)#战利品
huodejiangli = cv2.imread('./images/huodejiangli.png', 0)#获得奖励
zaizhanchang = cv2.imread('./images/zaizhanchang.png', 0)#在战场
haifangzhu = cv2.imread('./images/haifangzhu.jpg', 0)#海坊主
sx = cv2.imread('./images/sx.png', 0)#刷新
juexingcailiao = cv2.imread('./images/juexingcailiao.png', 0)#刷新
tz = cv2.imread('./images/tz.png', 0)#挑战
feng = cv2.imread('./images/fengqiling.png', 0)#刷新
lei = cv2.imread('./images/leiqiling.png', 0)#刷新
shui = cv2.imread('./images/shuiqiling.png', 0)#刷新
huo = cv2.imread('./images/huoqiling.png', 0)#刷新
# w, h = template.shape[::-1]


# im0 =ImageGrab.grab((0, 0, 2880, 1800))
# im0.show()
# im0 =ImageGrab.grab((0, 1500, 2880, 1800))
# im0.show()

# pyautogui.scroll(200) #鼠标滚轮   可以用负号

i=0
while 1:
    time1 = time.time()
    # time.sleep(2)
    img_rgb = np.array(ImageGrab.grab().convert('RGB'))#屏幕截图
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

    #探索
    res = cv2.matchTemplate(img_gray, ts, cv2.TM_CCOEFF_NORMED)# 大图 ，目标 ，模式
    threshold = 0.85  #阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0] ), int(pt[1] )) #鼠标移动到坐标位置
        pyautogui.click() #鼠标点击
        print("探索")
        break


    # 觉醒材料
    img_rgb = np.array(ImageGrab.grab().convert('RGB'))  # 屏幕截图
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    res = cv2.matchTemplate(img_gray, juexingcailiao, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.85 # 阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0]), int(pt[1]))  # 鼠标移动到坐标位置
        pyautogui.click()  # 鼠标点击
        print("点击御魂")
        time.sleep(random.randint(1, 2))
        break

    if random.randint(1, 4) == 3:
        cailiao=feng
        cailiao = lei
        cailiao = shui
        cailiao = huo
    cailiao = lei#######################################################################################################################################################################
    # 打材料
    res = cv2.matchTemplate(img_gray, cailiao, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.85  # 阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框
        pyautogui.moveTo(int(pt[0]), int(pt[1]))  # 鼠标移动到坐标位置
        pyautogui.click()  # 鼠标点击
        print("八岐大蛇")

        break


    # 组队
    res = cv2.matchTemplate(img_gray, tz, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.95  # 阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0]), int(pt[1]))  # 鼠标移动到坐标位置
        pyautogui.click()  # 鼠标点击
        print("组队")

        break


    # 创建队伍
    res = cv2.matchTemplate(img_gray, cjdw, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.95  # 阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0]), int(pt[1]))  # 鼠标移动到坐标位置
        pyautogui.click()  # 鼠标点击
        print("创建队伍")

        break


    # 创建
    res = cv2.matchTemplate(img_gray, cj, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.95  # 阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0]), int(pt[1]))  # 鼠标移动到坐标位置
        pyautogui.click()  # 鼠标点击
        print("创建")

        break

    #判断是不是有人加入
    img_rgb = np.array(ImageGrab.grab().convert('RGB'))  # 屏幕截图
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    res = cv2.matchTemplate(img_gray, yq, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.95  # 阈值 图片相识度
    loc = np.where(res >= threshold)

    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框
        if len(loc[0])!=3:
            time.sleep(random.randint(1, 2))
            # 开始战斗
            pyautogui.moveTo(1564, 895)  # 鼠标移动到坐标位置
            pyautogui.click()  # 鼠标点击
            print("人加入：", len(loc[0]))
            print("开始战斗")

        break


    img_rgb = np.array(ImageGrab.grab().convert('RGB'))  # 屏幕截图
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    # 准备
    res = cv2.matchTemplate(img_gray, zb, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.8  # 阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        time.sleep(random.randint(1, 30))
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0]), int(pt[1]))  # 鼠标移动到坐标位置
        pyautogui.click()  # 鼠标点击
        print("准备")
        break




    # 确定
    res = cv2.matchTemplate(img_gray, qd, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 0.8  # 阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0]), int(pt[1]))  # 鼠标移动到坐标位置
        pyautogui.click()  # 鼠标点击
        print("确定")

        break







    #结束
    res = cv2.matchTemplate(img_gray, js, cv2.TM_CCOEFF_NORMED)# 大图 ，目标 ，模式
    threshold = 0.8  #阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        time.sleep(random.randint(1, 3))
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0] ), int(pt[1] )) #鼠标移动到坐标位置
        pyautogui.click() #鼠标点击
        print("结束")
        time.sleep(random.randint(5, 120))
        break



    #失败
    res = cv2.matchTemplate(img_gray, sb, cv2.TM_CCOEFF_NORMED)# 大图 ，目标 ，模式
    threshold = 0.9  #阈值 图片相识度
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        time.sleep(random.randint(1, 3))
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )#在图片选中位置画线框

        pyautogui.moveTo(int(pt[0] ), int(pt[1] )) #鼠标移动到坐标位置
        pyautogui.click() #鼠标点击
        print("失败")
        time.sleep(random.randint(5, 120))
        break


    time2 = time.time()
    print(time2 - time1)

